package com.aoxon.cargo.adapter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.database.DataService;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.state.DataState;
import com.aoxon.cargo.state.MyStateUtil;

/**
 * 商家列表适配器
 * 
 * @author ZZJ
 * 
 */
public class PurSupplierListAdapter extends BaseAdapter implements MyAdapter{

	private boolean isBusy = false;
	private boolean showCheckBox = false;
	private List<Supplier> suppliers;
	private LayoutInflater layoutInflater;
	private HashMap<Supplier, Boolean> state = new HashMap<Supplier, Boolean>();
	private FileLoader myImageLoader = new FileLoader(100, 100, false);
	private DataService dataService;
	private String deviceId = null;
	public PurSupplierListAdapter(Context context, List<Supplier> suppliers,
			boolean showCheckBox) {
		this.suppliers = suppliers;
		this.showCheckBox = showCheckBox;
		this.layoutInflater = LayoutInflater.from(context);
		dataService = new DataService(context);
		try {
			TelephonyManager telMgr = (TelephonyManager) context.getSystemService(
					context.TELEPHONY_SERVICE);
			deviceId = telMgr.getDeviceId();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 
	 * @return 是否有删除数据
	 */
	public boolean deleteCheck() {
		boolean ischange = false;
		Iterator<Entry<Supplier, Boolean>> iter = state.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<Supplier, Boolean> entry = iter.next();
			Supplier key = entry.getKey();
			dataService.deleteCollection(deviceId, key.getSupId(), MyStateUtil.COLLECTION_SUPPLIER);
			suppliers.remove(key);
			ischange = true;
		}
		this.notifyDataSetChanged();
		return ischange;
	}
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public void resetList(List<?> list) {
		// TODO Auto-generated method stub
		if(list != null){
			this.suppliers.clear();
			this.suppliers = (List<Supplier>) list;
		}
		this.notifyDataSetChanged();
	}
	
	@Override
	public void setBusy(boolean isBusy) {
		// TODO Auto-generated method stub
		this.isBusy = isBusy;
	}

	@Override
	public void clearCache() {
		// TODO Auto-generated method stub
		if (myImageLoader != null) {
			myImageLoader.clearCache();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return suppliers.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return suppliers.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View converView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;

		if (converView == null) {
			viewHolder = new ViewHolder();
			converView = layoutInflater.inflate(
					R.layout.pub_supplier_information, null);
			viewHolder.cbChoose = (CheckBox) converView
					.findViewById(R.id.cbSupplierCheck);
			viewHolder.ivSupplierLogo = (ImageView) converView
					.findViewById(R.id.ivSupplierLogo);
			viewHolder.ivCertificate = (ImageView) converView
					.findViewById(R.id.ivSupplierCertification);
			viewHolder.tvStoreName = (TextView) converView
					.findViewById(R.id.tvSupplierStoreName);
			viewHolder.tvCompanyName = (TextView) converView
					.findViewById(R.id.tvSupplierCompanyName);
			viewHolder.tvAdress = (TextView) converView
					.findViewById(R.id.tvSupplierStoreAddress);
			viewHolder.progressBar = (ProgressBar) converView
					.findViewById(R.id.pbSupplierLogoLoading);
			converView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) converView.getTag();
		}

		try {

			final Supplier supplier = suppliers.get(position);

			String adress = supplier.getSupCity() + supplier.getsupMarket()
					+ supplier.getsupStall();

			viewHolder.tvStoreName.setText(supplier.getStoreName());
			viewHolder.tvCompanyName.setText(supplier.getSupCompanyFullName());
			viewHolder.tvAdress.setText(adress);
			viewHolder.ivSupplierLogo.setImageResource(R.drawable.loading);
			
			if (showCheckBox) {
				viewHolder.cbChoose.setChecked(state.get(position) == null ? false : true);
				viewHolder.cbChoose.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton arg0, boolean isCheck) {
						// TODO Auto-generated method stub
						if (isCheck) {
							state.put(supplier, isCheck);
						} else {
							state.remove(position);
						}
					}
				});
				viewHolder.cbChoose.setVisibility(View.VISIBLE);
			}

			if (supplier.getCerState() == DataState.SUCCESS) {
				viewHolder.ivCertificate.setVisibility(View.VISIBLE);
			} else {
				viewHolder.ivCertificate.setVisibility(View.GONE);
			}

			String path = supplier.getSupLogo().get(0).getPictureUrl()
					+ "s.png";
			viewHolder.ivSupplierLogo.setImageResource(R.drawable.loading);
			if (!isBusy) {
				myImageLoader.displayImage(path, viewHolder.ivSupplierLogo,
						viewHolder.progressBar, false);
			} else {
				myImageLoader.displayImage(path, viewHolder.ivSupplierLogo,
						viewHolder.progressBar, true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			viewHolder.progressBar.setVisibility(View.GONE);
		}

		return converView;
	}

	class ViewHolder {
		CheckBox cbChoose;
		ImageView ivSupplierLogo;
		ImageView ivCertificate;
		TextView tvStoreName;
		TextView tvCompanyName;
		TextView tvAdress;
		ProgressBar progressBar;
	}

}
